package com.example.demo.practice.ui;

import android.content.Context;
import android.graphics.Color;

import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.example.demo.practice.R;
import com.example.demo.practice.exception.NoMapException;
import com.example.demo.practice.innerclass.MWebView;
import com.example.demo.practice.tool.UnitConversion;

import java.util.ArrayList;
import java.util.List;


public class MapAndList extends RelativeLayout {
    private int height;//控件高度

    private Context context;
    private ListView expandableListView;
    private WebView webView;
    private LinearLayout bottom;
    private boolean fold;

    private LayoutParams params_bottom;
    private RelativeLayout relativeLayout;


    public MapAndList(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        DisplayMetrics displayMetrics = new DisplayMetrics();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(displayMetrics);
        height = displayMetrics.heightPixels;
        fold = true;
        initWebView();
        initCenterTool();

    }

    /**
     * 初始化地图
     */
    private void initWebView() {
        //地图容器
        webView = new WebView(context);
        addView(webView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        webView.setBackgroundColor(Color.RED);
        WebSettings webSettings = webView.getSettings();
        webSettings.setDefaultTextEncodingName("utf-8");
        webSettings.setAllowFileAccessFromFileURLs(true);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        webSettings.setBuiltInZoomControls(true);// 隐藏缩放按钮
        webSettings.setUseWideViewPort(true);// 可任意比例缩放
        webSettings.setLoadWithOverviewMode(true);// setUseWideViewPort方法设置webview推荐使用的窗口。setLoadWithOverviewMode方法是设置webview加载的页面的模式。

        webSettings.setSavePassword(true);
        webSettings.setSaveFormData(true);// 保存表单数据
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAppCacheEnabled(true);//缓存
        webSettings.setDomStorageEnabled(true);
        webSettings.setSupportMultipleWindows(true);// 新加
        webView.setDrawingCacheEnabled(true);
        webView.buildDrawingCache();
        webView.buildLayer();
        webSettings.setUseWideViewPort(true); // 关键点
        webSettings.setAllowFileAccess(true); // 允许访问文件
        webSettings.setSupportZoom(true); // 支持缩放
        webView.getSettings().setPluginState(WebSettings.PluginState.ON);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        webView.setWebViewClient(new MWebView());
    }

    /**
     * 初始化中间工具条
     */
    private void initCenterTool() {
        bottom = new LinearLayout(context);
        bottom.setOrientation(LinearLayout.VERTICAL);

        //中间标题
        relativeLayout = new RelativeLayout(context);
        IArcView iArcView = new IArcView(context);
        ImageView toggle = new ImageView(context);
        toggle.setImageDrawable(getResources().getDrawable(R.drawable.down));
        relativeLayout.addView(iArcView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        ViewGroup.LayoutParams iArcViewParams = iArcView.getLayoutParams();
        iArcViewParams.height = new UnitConversion().dpToPixel(40, context);
        relativeLayout.addView(toggle, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
        LayoutParams toggleParams = (LayoutParams) toggle.getLayoutParams();
        toggleParams.width = new UnitConversion().dpToPixel(30, context);
        toggleParams.height = new UnitConversion().dpToPixel(30, context);
        toggleParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
        bottom.addView(relativeLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        //可以下拉列表容器

        expandableListView = new ListView(context);
        expandableListView.setBackgroundColor(getResources().getColor(R.color.windowBackground));
        expandableListView.setDivider(null);
        bottom.addView(expandableListView, new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));


        addView(bottom, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        params_bottom = (LayoutParams) bottom.getLayoutParams();
        expandableListView.setBackgroundColor(Color.WHITE);
        params_bottom.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
        params_bottom.height = 2 * height / 5;

        toggle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                ImageView imageView = (ImageView) view;
                if (fold) {
                    int heightTop = relativeLayout.getLayoutParams().height;
                    imageView.setImageDrawable(getResources().getDrawable(R.drawable.up));
                    bottom.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                    setBottomHeight(heightTop, GONE);
                    fold = false;
                } else {
                    imageView.setImageDrawable(getResources().getDrawable(R.drawable.down));
                    bottom.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                    setBottomHeight(2 * height / 5, VISIBLE);
                    fold = true;
                }

            }
        });
    }

    /**
     * 更改底部参数
     *
     * @param height{int}底部列表高度
     * @param visibility{int}底部列表是否显示
     */
    private void setBottomHeight(int height, int visibility) {
        LayoutParams layoutParams = (LayoutParams) bottom.getLayoutParams();
        layoutParams.height = height;
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
        expandableListView.setVisibility(visibility);
    }

    public WebView getWebView() {
        return webView;
    }

    /**
     * 加载网页
     */
    public void loadUrl(String name) {
        webView.loadUrl("file:///android_asset/" + name);
    }

    /**
     * 设置地图中心
     *
     * @param lat
     * @param lng
     */
    public void setMapCenter(double lat, double lng) {
        webView.loadUrl("javascript:setCenter(" + lat + "," + lng + ")");
    }

    public void loadHtmlMethod(String method){
        webView.loadUrl("javascript:" +method);
    }

    public void setZoomCenter(double lat, double lng, int level) {
        webView.loadUrl("javascript:setZoomCenter(" + lat + "," + lng + "," + level + ")");
    }

    public ListView getExpandableListView() {
        return expandableListView;
    }


    public void setListAdapter(SimpleAdapter simpleAdapter) {
        expandableListView.setAdapter(simpleAdapter);
    }

    public void drawLine(String json){
        webView.loadUrl("javascript:drawLine(" +json + ")");
    }

    /**
     * 设置简单适配器
     *
     * @param arrayAdapter
     */
    public void setArrayAdapter(ArrayAdapter arrayAdapter) {
        expandableListView.setAdapter(arrayAdapter);
    }

    /**
     * 为点击列表设置监听
     *
     * @param onItemClickListener
     */
    public void setItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        expandableListView.setOnItemClickListener(onItemClickListener);
    }

    /**
     * 设置列表长按事件
     *
     * @param onItemLongClickListener
     */
    public void setItemLongClickListener(AdapterView.OnItemLongClickListener onItemLongClickListener) {
        expandableListView.setOnItemLongClickListener(onItemLongClickListener);
    }
}
